<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>开启头部工具栏 - 数据表格</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="${pageContext.request.contextPath }/back/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="${pageContext.request.contextPath }/back/style/admin.css" media="all">
</head>
<body>

  <div class="layui-card layadmin-header">
    <div class="layui-breadcrumb" lay-filter="breadcrumb">
      <a lay-href="">主页</a>
      <a><cite>组件</cite></a>
      <a><cite>数据表格</cite></a>
      <a><cite>开启头部工具栏</cite></a>
    </div>
  </div>
  
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
      <div class="layui-col-md12">
        <div class="layui-card">
          <div class="layui-card-header">用户列表</div>
          <div class="layui-card-body">
          	<div class="layui-form layui-card-header layuiadmin-card-header-auto">
        <div class="layui-form-item">
          <div class="layui-inline">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-block">
              <input type="text" name="loginname" placeholder="请输入" autocomplete="off" class="layui-input" id="userName">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
              <input type="text" name="telphone" placeholder="请输入" autocomplete="off" class="layui-input" id="userTel">
            </div>
          </div>
          <div class="layui-inline">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
              <input type="text" name="userName" placeholder="请输入" autocomplete="off" class="layui-input" id="userRealName">
            </div>
          </div>
          <div class="layui-inline">
            <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search" id="fuzzyQuery">
              <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
            </button>
          </div>
        </div>
      </div>
      
            <table class="layui-hide" id="test-table-toolbar" lay-filter="test-table-toolbar">

            </table>
            
            <script type="text/html" id="test-table-toolbar-toolbarDemo">
              <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
                <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
                <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
              </div>
            </script>
            
            <script type="text/html" id="buttonTpl">
                {{#  if(d.status == 1){ }}
                <button class="layui-btn layui-btn-xs">使用中</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-danger layui-btn-xs">已禁用</button>
                {{#  } }}
            </script>

            <script type="text/html" id="test-table-toolbar-barDemo">
              <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
              <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
            </script>
          </div>
        </div>
      </div>
    </div>
  </div>
  
  <script src="${pageContext.request.contextPath }/back/layui/layui.js"></script>

  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
  <script>
    function renderdata(userName,userRealName,userTel){
  layui.config({
    base: '${pageContext.request.contextPath }/back/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index', 'table'], function(){
    var admin = layui.admin
    ,table = layui.table;
  
    table.render({
      elem: '#test-table-toolbar'
      ,url: '${pageContext.request.contextPath }/userInfoController/getqueryAllUserInfo?userName=' + userName+'&userRealName='+userRealName+'&userTel='+userTel
      ,toolbar: '#test-table-toolbar-toolbarDemo'
      ,title: '用户数据表'
      ,cols: [[
        {type: 'checkbox', fixed: 'left'}
        ,{field:'userId', title:'ID', width:120, fixed: 'left', unresize: true, sort: true}
        ,{field:'userName', title:'用户名', width:150}
        ,{field:'userPwd', title:'密码', width:150}
        ,{field:'userImg', title:'头像', width:120}
        ,{field:'userRealName', title:'姓名', width:120}
        ,{field:'userTel', title:'手机号',width:210}
        ,{field:'userRealID', title:'身份证号', width:240}
        ,{field: 'status', title:'用户状态', templet: '#buttonTpl', width: 120 ,align: 'center'}
        ,{fixed: 'right', title:'操作', toolbar: '#test-table-toolbar-barDemo', width:150, align: 'center'}
      ]],
      request: {pageName: 'pageNum',limitName: 'pageSize'}
      ,page: true
      ,limit:5   //默认十条数据一页
      ,limits: [3, 5, 8, 10, 15, 20]
    });
    
    //头工具栏事件
    table.on('toolbar(test-table-toolbar)', function(obj){
      var checkStatus = table.checkStatus(obj.config.id);
      switch(obj.event){
        case 'getCheckData':
          var data = checkStatus.data;
          layer.alert(JSON.stringify(data));
        break;
        case 'getCheckLength':
          var data = checkStatus.data;
          layer.msg('选中了：'+ data.length + ' 个');
        break;
        case 'isAll':
          layer.msg(checkStatus.isAll ? '全选': '未全选');
        break;
      };
    });
    
    //监听行工具事件
    table.on('tool(test-table-toolbar)', function(obj){
      var data = obj.data;
      if(obj.event === 'del'){
        layer.confirm('真的删除行么', function(index){
         // obj.del();
          delCarouselById(data.userId,"0");
          layer.msg('删除成功');
          layer.close(index);
        });
      } else if(obj.event === 'edit'){
        if(data.status == 1){
          layer.confirm('确认初始化密码', function(index){
            //layer.alert(JSON.stringify(data));
            InitializeTheUserPwd(data.userId);
            layer.msg('密码：123456');
            layer.close(index);
          });
        }else {
          layer.confirm('确认启用该用户', function(index){
            //layer.alert(JSON.stringify(data));
            delCarouselById(data.userId,"1");
            layer.msg('启用成功');
            layer.close(index);
          });
        }
      }
    });
  
  });
    }

  //删除记录
  function delCarouselById(userId,status) {
    $.get("${pageContext.request.contextPath }/userInfoController/modifyStatus?userId=" + userId+"&status="+status,
            function(data, status) {
              $(".layui-laypage-btn").click() ;//刷新当前行
      });
  }
    //初始化密码
    function InitializeTheUserPwd(userId) {
      $.get("${pageContext.request.contextPath }/userInfoController/InitializeTheUserPwd?userId=" + userId,
              function(data, status) {
                $(".layui-laypage-btn").click() ;//刷新当前行
              });
    }
    $(function() {
      //渲染数据
      renderdata("","","");

      //模糊查询
      $("#fuzzyQuery").click(function(){
        var userName=$("#userName").val();
        var userRealName=$("#userRealName").val();
        var userTel=$("#userTel").val();

        renderdata(userName,userRealName,userTel);

      })

    })
  </script>
</body>
</html>